/*
 * mainRunner.cpp
 *
 *  Created on: 2009-10-20
 *      Author: steven
 */
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
#include <cstring>
#include <stdexcept>

using namespace std;

typedef string *pstring;

void testString()
{
          string s ,s1("world"),s2(10,'w');
          bool fake = true;
          string::size_type length;

          length = s2.size();

          cout << s.empty() << s2.size() << fake << endl;
          cout << s << s1 << s2 << endl;


          while(getline(cin,s))
          {
                  cout << s << endl;
                  cin >> s1 >> s2;
                  cout << s1 << s2 <<endl;
          }

}

void testVector()
{
        vector<string> mystr;
        mystr.push_back("this is our world!");
        mystr.push_back(string("hello"));
        mystr[1]="not good";

        cout << "The size is " << mystr.size() << "; Is empty?" << mystr.empty() <<endl;
        cout << "The second one is:" << mystr[1] << endl;

        vector<int> intArray(10);
        for ( vector<int>::size_type idx =0 ;idx != 10; idx++)
          {
            intArray[idx] = idx;
            cout << "Reinitilized : " << idx << " : " << intArray[idx] <<endl;
          }

        for ( vector<int>::iterator int_iter = intArray.begin(); int_iter != intArray.end();++int_iter)
          {
            *int_iter += 1;
            cout << "current iterator : " << *int_iter << endl;
          }

        for ( vector<int>::const_iterator cn_iter = intArray.begin(); cn_iter != intArray.end(); ++cn_iter)
          cout << "We can only retrieve this value,can not change : " << *cn_iter <<endl;
}

void testBitset()
{
  bitset<16> b(0xffff);
  bitset<32> a(0xffff);
  bitset<128> c(0xffff);

  cout << b << b.any() << b.count() << endl;
  cout << a << endl;
  cout << c << endl;

}

int testPointer()
{
  int iin = 1024;
  int iin1 = 2048;
  int *ipt = &iin;
  int *const iptc = &iin;

  cout << "The original number of pointer : " << *iptc << endl;

  //iptc = &iin1;



  int **iipt = &ipt;
  int ***iippp = &iipt;

  cout << iin << " ;The pointer : " << ipt << " ;The value :" << *ipt <<endl;
  cout << " The pointer's pointer : " << iipt << " ;The value :" << *iipt << endl;

  cout << " The three layer : " << iippp << " ;The value : " << *iippp << endl;

  int ia[] = {0,3,4,5,8};
  int *ip1 = ia;

  int *ip2 = ip1+4;
  ptrdiff_t length = ip2-ip1;

  cout << " The IP1 : " << *ip1 << ";The IP2: " << *ip2 << endl;
  cout << " The length of IP1 and IP2 : " << length << endl;

  return 0;

}

int testPString()
{
  string s1 = "abd";

  // This really means 'string const* p1', not 'const string *p1'
  // You can nor change the p1 to pointer otheres
  const pstring p1=&s1;

  //string s2 = "hello s2!";
  //p1 = &s2; cause error since the p1 can not be changed

  cout << *p1 << endl;

  const char *cp = "China";

  while(*cp)
    cout << *cp++ << endl;

  return 0;
}

void testCString()
{
  char mycstr[] = "This is";
  const char *mycstr1 = " The truth";

  char mys2[100];
  memset(mys2,0,100);

  cout << strlen(mycstr) << ";" << strcmp(mycstr,mycstr1) << endl;
  cout << "the cat : " << strncpy(mys2,mycstr,strlen(mycstr)) << endl;
  cout << strncat(mys2,mycstr1,strlen(mycstr1)) << endl;

}

void testDynamicArray()
{
  int *ptr = new int[10];
  int *ptr1 = new int[4]();
  string s1="Hi, this is our world!";

  const char *ptsr = s1.c_str();

  ptr[0] = 10;

  cout << *ptr << ";" << *ptr1 << ";" << ptsr << endl;

  delete [] ptr;
  delete [] ptr1;

}

void testMuliArray()
{
  int a[3][3]={{0,1,2},{3,4,5}};

  for ( int icount = 0; icount < 3 ;icount ++)
    for ( int jcount=0; jcount < 3; jcount ++)
      cout << "The array value : " << a[icount][jcount] << endl;

  int (*ip)[3] = a;


}


void testSizeOf()
{
    int i=1,*p=&i;
    char ch,str[10]="hello";

    //sizeof just get the type length of a varible, not the real value
    //e.g. sizeof(str), sizeof(*str)
    cout << sizeof(i) << sizeof(*p) << sizeof(ch) << ";" << sizeof(str)/sizeof(*str) << strlen(str) << endl;

}

void testException()
{
	static int a = 1;

	if (a/0)
	{
		//throw runtime_error("this is test of exception");
		cout << "How it works~~" << endl;
	}
}



int main()
{
        const size_t arrlen = 4;
        int arr[arrlen] = {0,1,2,3};
        vector<int> varr(arr, arr+arrlen);

        cout << varr.size() << ";" << varr.capacity() << endl;

        vector<int>::iterator myiter = varr.begin();

        for(;myiter != varr.end();myiter ++ )
          {
            cout << " The value is : " << *myiter << endl;
//            varr.push_back(*myiter+1);
          }

        //testString();
        //testVector();
        //testBitset();
        //testPointer();
        //testPString();
        //testCString();
        //testDynamicArray();
        //testMuliArray();
        //testSizeOf();
        try{
        	testException();
        }
        catch(runtime_error re)
        {
        	cout << "Encounter runtime error:" << re.what()<<endl;
        }
	return 0;
}
